<?php

declare(strict_types=1);

namespace App\Controller\m1;

use Throwable;
use App\Helper\Log;
use App\Helper\Util;
use App\Helper\Common;
use App\Helper\Verify;
use Hyperf\DbConnection\Db;
use App\Constants\ReturnCode;
use Hyperf\Di\Annotation\Inject;
use App\Controller\AbstractController;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\RequestMapping;
use Hyperf\HttpServer\Annotation\Middleware;
use App\Middleware\Manager\AuthMiddleware;

#[Controller]
#[Middleware(AuthMiddleware::class)]
class UserController extends AbstractController
{
    #用户列表
    #[RequestMapping(path: "list", methods: "post")]
    public function list()
    {
        $realname = $this->request->input('realname', '');
        $role = $this->request->input('role', '');
        $phone = $this->request->input('phone', '');
        $currentPage = $this->request->input('currentPage', 1);
        $pageSize = $this->request->input('pageSize', 15);
        $where = [];
        $user_id_where = [];
        if (!empty($realname)) {
            $where[] = ['realname', 'like', "%".trim($realname)."%"];
        }
        if (!empty($role)) {
            $user_caps = Db::table('office_member')
                ->where(['status'=>1,'is_del'=>0,'role'=>$role])
                ->pluck("user_id")
                ->toArray();
            $user_id_where = array_merge($user_id_where,$user_caps);
        }
        if (!empty($phone)) {
            $where[] = ['phone', '=', $phone];
        }
        $offset = ($currentPage - 1) * $pageSize;
        $columns = [
            'id',
            'avatar',
            'realname',
            'phone',
            'login_num',
            'login_time',
            'status',
            'created_at'
        ];
        $query = Db::table('user')
            ->select($columns)
            ->where($where);
        if(!empty($user_id_where)){
            $query->whereIn("id", $user_id_where);
        }
        $total = $query->count();
        $datas = $query->offset($offset)
            ->limit($pageSize)
            ->orderBy('created_at', 'desc')
            ->get()
            ->toArray();
        $return_list = [];
        if(!empty($datas)){
            $roleArr = ["cap"=>'护士长', "member"=>'护士'];
            foreach ($datas as $data) {
                $office_member = Db::table('office_member')
                    ->select(['office_id','role'])
                    ->where(['status'=>1,'is_del'=>0,'user_id'=>$data['id']])
                    ->first();
                $user_role = "-";
                $user_office = "-";
                if(!empty($office_member['role'])){
                    $user_role = !empty($roleArr[$office_member['role']])?$roleArr[$office_member['role']]:"-";
                }
                if(!empty($office_member['office_id'])){
                    $user_office = $office_member = Db::table('office')
                        ->where(['id'=>$office_member['office_id']])
                        ->value("name");
                }
                $return_list[] = [
                    'id'=>$data['id'],
                    'avatar'=>$data['avatar'],
                    'realname'=>$data['realname'],
                    'role'=>$user_role,
                    'phone'=>$data['phone'],
                    'office'=>$user_office,
                    'reg_time'=>date("Y-m-d H:i:s", $data['created_at']),
                    'login_time'=>date("Y-m-d H:i:s", $data['login_time'])
                ];
            }
        }
        return $this->fmt_response(ReturnCode::deal_succ, '成功', ['total'=>$total, 'list'=>$return_list]);
    }
    #用户详情
    #[RequestMapping(path: "detail", methods: "post")]
    public function detail()
    {
        $user_id = $this->request->input('user_id', 0);
        $office_id = Db::table('office_member')
            ->where(['status'=>1,'is_del'=>0,'user_id'=>$user_id])
            ->value("office_id");
        $columns = [
            'positional',
            'level',
            'education',
            'ycapital',
            'workno',
            'workbed',
            'remark'
        ];
        $data = Db::table('office_member_info')
            ->select($columns)
            ->where(['office_id'=>$office_id,'user_id'=>$user_id])
            ->first();
        $return_data = [];
        if(!empty($data)){
            $positional = "";
            if(!empty($data['positional'])){
                $positional = Db::table('conf_positional')
                    ->where("id", $data['positional'])
                    ->value("name");
            }
            $level = "";
            if(!empty($data['level'])){
                $level = Db::table('conf_nurse_level')
                    ->where("id", $data['level'])
                    ->value("name");
            }
            $education = "";
            if(!empty($data['education'])){
                $education = Db::table('conf_education')
                    ->where("id", $data['education'])
                    ->value("name");
            }
            $return_data = [
                'positional'=>$positional,
                'level'=>$level,
                'education'=>$education,
                'ycapital'=>$data['ycapital'],
                'workno'=>$data['workno'],
                'workbed'=>$data['workbed'],
                'remark'=>$data['remark']
            ];
        }
        return $this->fmt_response(ReturnCode::deal_succ, '成功', $return_data);
    }
}